package com.aorise.controller.system;

import com.aorise.controller.BaseController;
import com.aorise.exception.DataValidationException;
import com.aorise.model.request.UpdatePwdReq;
import com.aorise.model.response.UserInfoRes;
import com.aorise.utils.*;
import com.aorise.utils.json.JsonResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: zhouhao
 * @Date: 2019/5/29
 * @Description: 个人中心
 */
@Slf4j
@Api(description = "个人中心接口")
@RestController
@Validated
@RequestMapping("api/userInfo")
public class UserInfoController extends BaseController{

    /**
     * 个人中心
     * HTTP 方式：GET
     * API 路径：/api/userInfo/
     * 方法名：userList
     * 方法返回类型：String
     */
    @ApiOperation(value = "登录用户详情", httpMethod = "GET", response = String.class, notes = "个人中心")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token",  paramType = "query", dataType = "string")
    })
    @RequestMapping(value = "/userInfo", method = RequestMethod.GET,produces="application/json")
    public String userInfo(String token){
        UserInfoRes userInfo =null;
        try {
            userInfo = userService.selectUserInfoByUserId(null);
        }catch (DataValidationException e) {
            log.error(
                    "controller:UserInfoController. function:userInfo...msg: userInfo DataValidationException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DATA_FORMAT_ERROR,
                    e.getMessage(), null).toString();
        }catch (DataAccessException e) {
            log.error(
                    "controller:UserInfoController. function:userInfo...msg: userInfo DataAccessException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:UserInfoController. function:userInfo. msg:userInfo Exception");
            log.error(e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
        return JsonResponseData.ofSuccess(userInfo);
    }

    /**
     * 密码修改
     * HTTP 方式：POST
     * API 路径：/api/userInfo/
     * 方法名：changePwd
     * 方法返回类型：String
     */
    @ApiOperation(value = "密码修改", httpMethod = "POST", response = String.class, notes = "密码修改")
    @RequestMapping(value = "/changePwd", method = RequestMethod.POST,produces="application/json")
    public String changePwd(@RequestBody @Validated UpdatePwdReq pwd,BindingResult result) {
        try {
            String x = getString(pwd, result);
            if (x != null){
                return x;
            }
        }catch (DataValidationException e) {
            log.error(
                    "controller:UserInfoController. function:changePwd...msg: changePwd DataValidationException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DATA_FORMAT_ERROR,
                    e.getMessage(), null).toString();
        }catch (DataAccessException e) {
            log.error(
                    "controller:UserInfoController. function:changePwd...msg: changePwd DataAccessException");
            log.error("error:" + e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.DB_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.DB_ERROR), null).toString();
        } catch (Exception e) {
            log.error(
                    "controller:UserInfoController. function:changePwd. msg:changePwd Exception");
            log.error(e.getMessage());
            return new JsonResponseData(false, "", StatusDefine.SYS_ERROR,
                    StatusDefineMessage.GetMessage(StatusDefine.SYS_ERROR), null).toString();
        }
        return JsonResponseData.ofFailure();
    }
}
